package com.japps.turmadochaves.adapter;

import java.util.ArrayList;

import com.japps.turmadochaves.classes.Personagem;
import com.japps.turmadochaves.prefs.HelpersPrefs;
import com.japps.turmadochaves.R;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

public class ExpandableAdapter extends BaseExpandableListAdapter {

	private Context context;
	private ArrayList<String> groups;
	private ArrayList<ArrayList<Personagem>> children;
		
	public ExpandableAdapter(Context context, ArrayList<String> groups,
			ArrayList<ArrayList<Personagem>> children) {
		super();
		this.context = context;
		this.groups = groups;
		this.children = children;
	}

	public void addItem(Personagem personagem) {
		if (!groups.contains(personagem.getNome())) {
			groups.add(personagem.getNome());
		}
		int index = groups.indexOf(personagem.getNome());
		if (children.size() < index + 1) {
			children.add(new ArrayList<Personagem>());
		}
		children.get(index).add(personagem);
	}

	@Override
	public boolean areAllItemsEnabled() {
		return true;
	}

	public Object getChild(int groupPosition, int childPosition) {
		return children.get(groupPosition).get(childPosition);
	}

	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		final Personagem personagem = (Personagem) getChild(groupPosition,
				childPosition);
		if (convertView == null) {
			LayoutInflater infalInflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = infalInflater.inflate(R.layout.applist_child, null);
		}
		CheckBox chk = (CheckBox) convertView.findViewById(R.id.chkChild);
		chk.setText("   " + personagem.getSom());
		chk.setChecked(HelpersPrefs.getBooleanPrefs(context, personagem.getId()));

		chk.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				try {																								
					
					HelpersPrefs.addBooleanPrefs(context, personagem.getId(), isChecked);
					
					if (isChecked) {								
						
						Uri path = Uri.parse("android.resource://com.japps.turmadochaves/raw/" + personagem.getId());

						MediaPlayer mediaPlayer = new MediaPlayer();
						mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
						mediaPlayer.setDataSource(context.getApplicationContext(), path);
						mediaPlayer.prepare();
						mediaPlayer.start();
					}
				} catch (Exception e) {
					Toast.makeText(context, "Ocorreu um erro!", 10000).show();
				}
			}
		});

		return convertView;
	}

	public int getChildrenCount(int groupPosition) {
		return children.get(groupPosition).size();
	}

	public Object getGroup(int groupPosition) {
		return groups.get(groupPosition);
	}

	public int getGroupCount() {
		return groups.size();
	}

	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		String group = (String) getGroup(groupPosition);
		if (convertView == null) {
			LayoutInflater infalInflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = infalInflater.inflate(R.layout.applist_group, null);
		}
		TextView tv = (TextView) convertView.findViewById(R.id.tvGroup);
		tv.setText(group);
		return convertView;
	}

	public boolean hasStableIds() {
		return true;
	}

	public boolean isChildSelectable(int arg0, int arg1) {
		return true;
	}

}
